package Models;

import DB.DBCustomer;
import DB.DBLocation;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;

public class Customer implements DB.RowInterface
{
	private int customerNo;
	private String name = "";
	private int locationNo;
	private Location location = new Location();
	private int cvr;
	private int phoneNo;
	private ArrayList<Location> locations = new ArrayList<Location>();

	public Customer ()
	{}

	public void importData(ResultSet data) throws SQLException
	{
		this.setCustomerNo(data.getInt("customerNo"));
		this.setName(data.getString("name"));
		this.setLocationNo(data.getInt("locationNo"));
		this.setCvr(data.getInt("cvr"));
		this.setPhoneNo(data.getInt("phoneNo"));

		if(getLocationNo() != 0)
		{
			DBLocation dbl = new DBLocation();
			Location locationRow = dbl.fetchByNo(getLocationNo());
			if(locationRow == null) locationRow = new Location();
			this.setLocation(locationRow);
		}
	}

	public void fetchSubItems()
	{
		DBLocation dbl = new DBLocation();
		ArrayList<Location> locationRows = dbl.fetchAll("customerNo = "+getCustomerNo(), "zipCode ASC");
		if(locationRows == null) locationRows = new ArrayList<Location>();
		this.setLocations(locationRows);
	}

	public HashMap exportData()
	{
		HashMap data = new HashMap();
		data.put("name", getName());
		data.put("locationNo", getLocationNo());
		data.put("cvr", getCvr());
		data.put("phoneNo", getPhoneNo());
		return data;
	}

	public void setData(String nameS, String cvrS, String phoneNoS) throws EmptyValueException, NumberFormatException
	{
		if(!nameS.equals("") && !cvrS.equals("") && !phoneNoS.equals(""))
		{
			this.setName(nameS);

			int cvrTemp = Integer.parseInt(cvrS);
			this.setCvr(cvrTemp);

			int phoneNoTemp = Integer.parseInt(phoneNoS);
			this.setPhoneNo(phoneNoTemp);
		}
		else
		{
			throw new EmptyValueException();
		}
	}

	public void refreshData()
	{
		Customer customer = new DBCustomer().fetchByNo(getCustomerNo());
		this.setName(customer.getName());
		this.setCvr(customer.getCvr());
		this.setPhoneNo(customer.getPhoneNo());
		this.setLocationNo(customer.getLocationNo());
		this.setLocations(customer.getLocations());
	}

	public static String[] getSearchColumns()
	{
		return new String[] { "customerNo", "name", "address", "zipCode", "city", "cvr", "phoneNo" };
	}
	
	public boolean addLocation(Location location)
	{
		location.setCustomerNo(getCustomerNo());
		return locations.add(location);
	}

	public boolean removeLocation(Location location)
	{
		int i = 0;
		while(i <= getLocations().size())
		{
			if(location.getLocationNo() == getLocations().get(i).getLocationNo())
			{
				location.setCustomerNo(0);
				return locations.remove(getLocations().get(i));
			}
			i++;
		}
		return false;
	}

	public void setCustomerNoOnLocations()
	{
		for(Location locationE : locations)
		{
			locationE.setCustomerNo(customerNo);
		}
	}

	public int getCustomerNo()
	{
		return customerNo;
	}

	public int getCvr()
	{
		return cvr;
	}

	public Location getLocation()
	{
		return location;
	}

	public int getLocationNo()
	{
		return locationNo;
	}

	public ArrayList<Location> getLocations()
	{
		return locations;
	}

	public String getName()
	{
		return name;
	}

	public int getPhoneNo()
	{
		return phoneNo;
	}

	public void setCustomerNo(int customerNo)
	{
		this.customerNo = customerNo;
	}

	public void setCvr(int cvr)
	{
		this.cvr = cvr;
	}

	public void setLocation(Location location)
	{
		this.location = location;
		this.setLocationNo(location.getLocationNo());
	}

	public void setLocationNo(int locationNo)
	{
		this.locationNo = locationNo;
	}

	public void setLocations(ArrayList<Location> locations)
	{
		this.locations = locations;
	}

	public void setName(String name)
	{
		this.name = name;
	}

	public void setPhoneNo(int phoneNo)
	{
		this.phoneNo = phoneNo;
	}
}